package api

import (
	"context"
	"getaway/basic/config"
	__ "getaway/basic/proto/orderproto"
	"getaway/handler/request"
	"getaway/handler/response"
	"getaway/middleware"
	"github.com/gin-gonic/gin"
)

// DoubtAdd
// @Summary      订单疑问添加
// @Description  订单疑问添加
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.DoubtAdd    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/doubtAdd [post]
// @Security TokenAuth
func DoubtAdd(c *gin.Context) {
	userId := c.GetUint("userId")
	authorityId := c.GetUint("authorityId")
	if authorityId != 1 {
		response.SrvError(c, "订单疑问添加失败", "只有用户才能添加订单疑问")
		return
	}
	var req request.DoubtAdd
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	add, err := config.OrderClient.DoubtAdd(context.Background(), &__.DoubtAddReq{
		OrderId:     req.OrderId,              //订单id
		UserId:      uint64(userId),           //操作人
		LaterAmount: float32(req.LaterAmount), //调帐后的价格
		Reason:      req.Reason,               //原因
	})
	if err != nil {
		response.SrvError(c, "订单疑问添加失败", err.Error())
		return
	}
	
	// 记录订单疑问添加行为
	middleware.RecordClickBehavior(c, userId, "订单疑问添加", map[string]interface{}{
		"orderId": req.OrderId,
		"reason": req.Reason,
		"laterAmount": req.LaterAmount,
		"success": err == nil,
	})
	
	response.Success(c, "订单疑问添加成功", add)
}

// DoubtDel
// @Summary      订单疑问删除
// @Description  订单疑问删除
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.DoubtDel    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Security TokenAuth
// @Router       /order/doubtDel [post]
func DoubtDel(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.DoubtDel
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	_, err := config.OrderClient.DoubtDel(context.Background(), &__.DoubtDelReq{
		Id:     req.Id,
		UserId: uint64(userId),
	})
	if err != nil {
		response.SrvError(c, "订单疑问删除失败", err.Error())
		return
	}
	
	// 记录订单疑问删除行为
	middleware.RecordClickBehavior(c, userId, "订单疑问删除", map[string]interface{}{
		"doubtId": req.Id,
		"success": err == nil,
	})
	
	response.Success(c, "订单疑问删除成功", nil)
}

// DoubtUpdate
// @Summary      订单疑问修改
// @Description  订单疑问修改
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.DoubtUpdate    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Security TokenAuth
// @Router       /order/doubtUpdate [post]
func DoubtUpdate(c *gin.Context) {
	userId := c.GetUint("userId")
	authorityId := c.GetUint("authorityId")
	if authorityId != 1 {
		response.SrvError(c, "订单疑问修改失败", "只有用户才能修改订单疑问")
		return
	}
	var req request.DoubtUpdate
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	update, err := config.OrderClient.DoubtUpdate(context.Background(), &__.DoubtUpdateReq{
		Id:          req.Id,
		UserId:      uint64(userId),
		LaterAmount: float32(req.LaterAmount),
	})
	if err != nil {
		response.SrvError(c, "订单疑问修改失败", err.Error())
		return
	}
	
	// 记录订单疑问修改行为
	middleware.RecordClickBehavior(c, userId, "订单疑问修改", map[string]interface{}{
		"doubtId": req.Id,
		"laterAmount": req.LaterAmount,
		"success": err == nil,
	})
	
	response.Success(c, "订单疑问修改成功", update.Id)
}

// DoubtList
// @Summary      订单疑问展示
// @Description  订单疑问展示
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   query    request.DoubtList    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Security TokenAuth
// @Router       /order/doubtList [get]
func DoubtList(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.DoubtList
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	list, err := config.OrderClient.DoubtList(context.Background(), &__.DoubtListReq{
		UserId:   uint64(userId),
		Page:     req.Page,
		PageSize: req.PageSize,
		Status:   req.Status,
	})
	if err != nil {
		response.SrvError(c, "订单疑问展示失败", err.Error())
		return
	}
	
	// 记录订单疑问列表查询行为
	middleware.RecordClickBehavior(c, userId, "订单疑问列表查询", map[string]interface{}{
		"status": req.Status,
		"page": req.Page,
		"pageSize": req.PageSize,
		"success": err == nil,
	})
	
	response.Success(c, "订单疑问展示成功", list)
}

// DoubtDetail
// @Summary      订单疑问详情
// @Description  订单疑问详情
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   query    request.DoubtDetail    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Security TokenAuth
// @Router       /order/doubtDetail [get]
func DoubtDetail(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.DoubtDetail
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	detail, err := config.OrderClient.DoubtDetail(context.Background(), &__.DoubtDetailReq{
		Id:     req.Id,
		UserId: uint64(userId),
	})
	if err != nil {
		response.SrvError(c, "订单疑问详情失败", err.Error())
		return
	}
	
	// 记录订单疑问详情查询行为
	middleware.RecordClickBehavior(c, userId, "订单疑问详情查询", map[string]interface{}{
		"doubtId": req.Id,
		"success": err == nil,
	})
	
	response.Success(c, "订单疑问详情成功", detail)
}

// CouponCreate
// @Summary      优惠券创建
// @Description  优惠券创建
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.CouponCreate    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Security TokenAuth
// @Router       /order/couponCreate [post]
func CouponCreate(c *gin.Context) {
	userId := c.GetUint("userId")
	authorityId := c.GetUint("authorityId")
	if authorityId != 3 {
		response.SrvError(c, "优惠券创建失败", "只有管理员才能创建优惠券")
		return
	}
	var req request.CouponCreate
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}

	create, err := config.OrderClient.CouponCreate(context.Background(), &__.CouponCreateReq{
		CouponType: req.CouponType,
		Price:      float32(req.Price),
		OrderId:    req.OrderId,
		OperatorId: uint64(userId),
	})
	if err != nil {
		response.SrvError(c, "优惠券创建失败", err.Error())
		return
	}
	
	// 记录优惠券创建行为
	middleware.RecordClickBehavior(c, userId, "优惠券创建", map[string]interface{}{
		"couponType": req.CouponType,
		"price": req.Price,
		"orderId": req.OrderId,
		"success": err == nil,
	})
	
	response.Success(c, "优惠券创建成功", create.Id)
}
